<template>
  <el-popover
    placement="right"
    width="600"
    trigger="click"
    @show="open"
    ref="popover"
  >
    <h5>专家选择</h5>
    <el-input v-model="name" slot="reference" clearable />

    <el-input
      placeholder="请输入内容"
      v-model="queryParams.condition.name"
      size="small"
      clearable
    >
      <template slot="append"
        ><el-button @click="()=>{query(1)}" size="small">搜索</el-button></template
      >
    </el-input>

    <edu-table
      ref="tableList"
      :data="tableList"
      :pageSize="queryParams.pageSize"
      :pageNum="queryParams.pageNum"
      :total="queryParams.total"
      @page-change="handleCurrentChange"
      @rowClick="rowClick"
      :order="true"
      :selection="false"
    >
      <el-table-column label="标题" prop="title" />
      <el-table-column label="专家名称" prop="expertName" />
      <el-table-column label="专家领域" prop="fieldName" />
    </edu-table>

    <div class="flex buttons">
      <el-button @click="close" size="small">关闭</el-button>
      <el-button @click="submit" type="primary" size="small">确认</el-button>
    </div>
  </el-popover>
</template>

<script>
import { queryKnowledgeCourseList } from "@/api/knowledge";
import utils from "@/utils";

export default {
  props: ["value", "valueText"],
  data() {
    return {
      name: "",
      queryParams: {
        condition: {
          name: "",
          publishStatus: 1, //已发布
        },
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      selectvalue: "",
      formData: {
        name: "",
      },
      tableList: [],
    };
  },
  created() {
    this.name = this.valueText;
  },
  watch: {
    valueText(newValue, oldValue) {
      this.name = newValue;
    },
  },
  methods: {
    open() {
      this.query(1);
    },
    close() {
      let popover = this.$refs.popover;
      popover.doClose();
    },
    handleCurrentChange(page) {
      this.queryParams.pageSize = page.pageSize;
      this.queryParams.pageNum = page.pageNum;

      this.query();
    },
    async query(pageNum) {
      // 有pageNum 重置为第一页
      if(pageNum){
        this.queryParams.pageNum = pageNum
      }

      let { returnObj } = await queryKnowledgeCourseList(this.queryParams);

      this.tableList = returnObj.list;
      this.queryParams.total = returnObj.total;
      this.queryParams.pageSize = returnObj.pageSize;
    },
    // 获取id
    async rowClick(row) {
      this.formData = row;
    },
    async submit() {
      this.close();

      this.name = this.formData.title;
      this.$emit("input", this.formData.id);
    },
  },
};
</script>

<style lang="scss" scoped>
.buttons {
  justify-content: flex-end;
}
</style>
